<!--
 * @Description: 
 * @Date: 2024-05-30 10:46:11
 * @LastEditTime: 2024-06-14 16:34:58
-->
<template>
  <Dialog :title="dialogTitle" v-model="redeployVisible">
    <div class="pb-50px">
      <Search
        :schema="allSchemas.searchSchema"
        @search="setSearchParams"
        @reset="setSearchParams"
      />
      <Table
        :columns="allSchemas.tableColumns"
        :data="tableObject.tableList"
        :pagination="{
          total: tableObject.total
        }"
        v-model:pageSize="tableObject.pageSize"
        v-model:currentPage="tableObject.currentPage"
      >
        <template #action="{ row }">
          <el-button link type="primary" @click="appoint(row.id, row.nickname)"> 指定他 </el-button>
        </template>
      </Table>
    </div>
    <!-- <template #footer>
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="redeployVisible = false">取 消</el-button>
    </template> -->
  </Dialog>
</template>
<script setup lang="ts">
import * as CaseQueryRecordApi from '@/api/pro/casequeryrecord'
import * as intervieWappointmentApi from '@/api/pro/interviewappointment'
import * as PetitionRequestRecordApi from '@/api/pro/petitionrequestrecord'
import * as DetentionReviewCenterApi from '@/api/pro/detentionreviewcenter'
import * as PublicInterestLitigationApi from '@/api/pro/publicinterestlitigation'
import { useUserStore } from '@/store/modules/user'
import { allSchemas } from './CaseQueryRecordRedeploy.data'
import * as RoleApi from '@/api/system/role'

let typeList = {
  anJian: 'anjian',
  jieFang: 'jiancha',
  xinFang: 'xinFang',
  jiYa: 'jiYa',
  gongYi: 'gongYi'
}

let roleId = undefined

const props = defineProps(['type'])
const emit = defineEmits(['success'])
const { tableObject, tableMethods } = useTable({
  getListApi: CaseQueryRecordApi.getSystemList // 分页接口
  // defaultParams: { id: roleId }
})
let redeployId = ref()
let roleList = []
const getRoleId = (type) => {
  let arr = roleList.filter((item) => {
    return item.code == type
  })

  return arr.length > 0 ? arr[0]['id'] : undefined
}
const message = useMessage()
const { getList, setSearchParams } = tableMethods
const dialogTitle = ref('转派') // 弹窗的标题
const redeployVisible = ref(false)
const open = async (id: number, accept) => {
  const data = await RoleApi.getRolePage()
  roleList = data.list
  roleId = getRoleId(typeList[props.type])
  tableObject.params = { id: roleId }
  redeployVisible.value = true
  redeployId.value = id
  // 修改时，设置数据
  getList()
}

const appoint = async (id: number, nickname: string) => {
  if (props.type == 'jieFang') {
    let data = await intervieWappointmentApi
      .updateInterviewAppointment({
        id: redeployId.value,
        acceptUserId: id,
        acceptUserName: nickname,
        businessType: 'jieFang'
      })
      .then((res) => {
        message.success('指定成功')
        redeployVisible.value = false
        emit('success')
      })
  } else if (props.type == 'xinFang') {
    let data = await PetitionRequestRecordApi.updatePetitionRequestRecord({
      id: redeployId.value,
      acceptUserId: id,
      acceptUserName: nickname,
      businessType: 'xinFang'
    }).then((res) => {
      message.success('指定成功')
      redeployVisible.value = false
      emit('success')
    })
  } else if (props.type == 'jiYa') {
    let data = await DetentionReviewCenterApi.updateDetentionReviewCenter({
      id: redeployId.value,
      acceptUserId: id,
      acceptUserName: nickname,
      businessType: 'gongYi'
    }).then((res) => {
      message.success('指定成功')
      redeployVisible.value = false
      emit('success')
    })
  } else if (props.type == 'gongYi') {
    let data = await PublicInterestLitigationApi.updatePublicInterestLitigation({
      id: redeployId.value,
      acceptUserId: id,
      acceptUserName: nickname,
      businessType: 'gongYi'
    }).then((res) => {
      message.success('指定成功')
      redeployVisible.value = false
      emit('success')
    })
  } else {
    let data = await CaseQueryRecordApi.updateCaseQueryRecord({
      id: redeployId.value,
      acceptUserId: id,
      acceptUserName: nickname,
      businessType: 'anJian'
    }).then((res) => {
      message.success('指定成功')
      redeployVisible.value = false
      emit('success')
    })
  }
}

defineExpose({ open })
onMounted(() => {})
</script>
